package com.zhengdongzulin.zuke.ext

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import android.provider.MediaStore
import android.widget.ImageView
import androidx.core.os.EnvironmentCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.easyadapter.ViewHolder
import com.xuexiang.xui.utils.DensityUtils
import com.zhengdongzulin.zuke.base.MyApp
import java.io.File
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.*

/**
 * 加载图片
 */
fun ImageView.loadImage(url: String?, errId: Int = 0, radius: Float = 0f) {
    Glide.with(context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(this)
}

/**
 * 加载图片
 */
fun BaseViewHolder.loadImage(viewId: Int, url: String?, errId: Int = 0, radius: Float = 0f) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(view)
}

/**
 * 加载图片
 */
fun ViewHolder.loadImage(viewId: Int, url: String?, errId: Int = 0, radius: Float = 0f) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(view)
}

/**
 * 加载圆型图片
 */
fun ImageView.loadCircleImage(url: String?, errId: Int = 0) {
    Glide.with(context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(MyApp.getCircleGlideOptions(errId)).into(this)
}

/**
 * 加载圆型图片
 */
fun BaseViewHolder.loadCircleImage(viewId: Int, url: String?, errId: Int = 0) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(MyApp.getCircleGlideOptions(errId)).into(view)
}

/**
 * 加载圆型图片
 */
fun ViewHolder.loadCircleImage(viewId: Int, url: String?, errId: Int = 0) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(if (!url.isNullOrEmpty()) url else errId)
            .apply(MyApp.getCircleGlideOptions(errId)).into(view)
}

/**
 * 加载本地资源图片
 */
fun ImageView.loadImage(resId: Int?, errId: Int = 0, radius: Float = 0f) {
    Glide.with(context).load(this.context.getDrawable(resId!!))
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(this)
}

/**
 * 加载本地资源图片
 */
fun BaseViewHolder.loadImage(viewId: Int, resId: Int?, errId: Int = 0, radius: Float = 0f) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(view.context.getDrawable(resId!!))
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(view)
}
/**
 * 加载本地资源图片
 */
fun ViewHolder.loadImage(viewId: Int, resId: Int?, errId: Int = 0, radius: Float = 0f) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(view.context.getDrawable(resId!!))
            .apply(
                    MyApp.getGlideOptions(
                            errId,
                            if (radius <= 0f) radius.toInt() else DensityUtils.dp2px(radius)
                    )
            ).into(view)
}

/**
 * 加载本地资源圆型图片
 */
fun ImageView.loadCircleImage(resId: Int?, errId: Int = 0) {
    Glide.with(context).load(this.context.getDrawable(resId!!))
            .apply(MyApp.getCircleGlideOptions(errId)).into(this)
}

/**
 * 加载本地资源圆型图片
 */
fun BaseViewHolder.loadCircleImage(viewId: Int, resId: Int?, errId: Int = 0) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(view.context.getDrawable(resId!!))
            .apply(MyApp.getCircleGlideOptions(errId)).into(view)
}

/**
 * 加载本地资源圆型图片
 */
fun ViewHolder.loadCircleImage(viewId: Int, resId: Int?, errId: Int = 0) {
    val view = getView<ImageView>(viewId)
    Glide.with(view.context).load(view.context.getDrawable(resId!!))
            .apply(MyApp.getCircleGlideOptions(errId)).into(view)
}

/**
 * 保存图片到相册
 */
fun Bitmap.saveImageToGallery(context: Context): Boolean {
    var file = createImageFile(context)
    // 首先保存图片
    if (file == null) {
        return false
    }
    try {
        val fos = FileOutputStream(file)
        this?.compress(Bitmap.CompressFormat.JPEG, 100, fos)
        fos.flush()
        fos.close()
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
    // 其次把文件插入到系统图库
    try {
        MediaStore.Images.Media.insertImage(
                context.contentResolver,
                file!!.absolutePath,
                file.name,
                null
        )
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
    context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))
    return true
}

fun createImageFile(context: Context): File? {
    val imageName: String =
            SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    if (!storageDir!!.exists()) {
        storageDir.mkdir()
    }
    val tempFile = File.createTempFile(imageName, ".jpg", storageDir)
    return if (Environment.MEDIA_MOUNTED != EnvironmentCompat.getStorageState(tempFile)) {
        null
    } else tempFile
}